<?php

namespace app\panel\controller;
use think\Controller;

class Login extends Controller
{
    public function initialize()
    {
        $this->model = model('Common/Admin');
    }

    // 登录
    public function index()
    {
        // 临时关闭当前模板的布局功能
        $this->view->engine->layout(false);

        return $this->fetch();
    }

    // 执行登录操作
    public function login_post()
    {
        if (request()->isPost()) {
            $data = input('post.');

            // 验证数据
            $result = $this->validate($data, 'Admin.login');
            if(true !== $result){
                // 验证失败 输出错误信息
                $this->error($result);
            }
            if(!captcha_check($data['captcha'], '', config('captcha'))){
                //验证失败
                $this->error('验证码错误或失效');
            }
            $user = $this->model->login($data['account'], $data['password']);

            if ($user == false) {
                $this->error($this->model->getError());
            }

            $this->success('登录成功', Url('index/index'));
        } else {
            $this->error('请求错误');
        }
    }

    // 退出登录
    public function logout()
    {
        session(null);
        $this->success('退出成功！', Url('index'));
    }
}
